Overload subscript operator [] in C ++
The subscript operator [] in C ++ is often used to access array elements. This operator can be overloaded to enhance an existing feature of arrays in C ++ (so it can be called an array index operator).
The following example illustrates how to load the subscript operator [] in C ++:
#include using namespace std ; const int KICHCO = 15 ; class ViDuMang { private : int mang [ KICHCO ]; public : ViDuMang () { register int i ; for ( i = 0 ; i < KICHCO ; i ++) { mang [ i ] = i ; } } int & operator []( int i ) { if ( i > KICHCO ) { cout << "n======================n" << endl ; cout << "Chi muc vuot gioi han!!" << endl ; // Tra ve phan tu dau tien. return mang [ 0 ]; } return mang [ i ]; } }; int main () { ViDuMang V ; cout << "Gia tri cua V[3] la: " << V [ 3 ] << endl ; cout << "Gia tri cua V[6] la: " << V [ 6 ]<< endl ; cout << "Gia tri cua V[16] la: " << V [ 16 ]<< endl ; return 0 ; }
Compiling and running the above C ++ program will produce the following results:
According to Tutorialspoint
Previous article: Load the operator stack call the function () in C ++
Next lesson: Overload member access operator (->) in C ++
4 ★ | 1 Vote
You should read it
May be interested
- The '+' operator in SQL Serverlearn how to use the + operator in sql server to join two or more strings into a string.
- Load the operator stack to call the function () in C ++c ++ function call () can be overloaded for objects of class type. when you overload (), you are not creating a new way to call a function.
- How to write subscript numbers in PowerPointpreviously tipsmake instructed you to read how to write exponents in word and write subscript in excel. in powerpoint, you can also write the characters above and below in the slide to present content, especially to present content in mathematics or other subjects.
- How to fix CPU overload error 99%, 100% simple, effective!how to fix cpu overload error 99%, 100%? this article will help you solve the problem of computer overload quickly and effectively!
- Overload relational operator in C ++there are many different relational operators supported by c ++, such as: (, =, ==, ...) that can be used to compare the data types available in c ++.
- Fix CPU and 99% CPU overload errors on Windowsif your computer's fan is constantly running at high speed, your cpu is overloaded. this means your computer will slow down. that is really annoying if you don't fix it.
- Instructions for creating superscript, subscript in Excelthe article i will guide you to write the superscript and subscript in excel.
- How to fix OneDrive error makes CPU overload on Windows 10the error of opening onedrive causes cpu overload to occur frequently on windows 10 creators, causing the device to be in a state of 'stall' continuously.
- Load the Input / Output operator stack in C ++c ++ is able to input and output existing data types by using thread extraction operators >> and thread insertion operators
- How to Get Compensation from a BUI Accidentif you were injured in a boating accident, then you can sue the boat operator who caused the injury. for example, the operator may have hit your own boat, or you may have been in the boat with the operator who was intoxicated. either way,...